/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;

/**
 *
 * @author Main
 */
public class TextButton extends GUIButton {
    protected String unselectedText = null;

    public TextButton(String label, Color color, Font font) {

        super(color);
        this.setMargin(new Insets(1, 2, 1, 2));
        this.setNegative(true);
        this.setHasBorder(true);
        this.setFont(font);
        this.setText(label);
    }

    @Override
    protected void drawImage(Graphics g) {
        int textY = (g.getFontMetrics().getAscent()+getHeight()-4)/2;
        int textX = (getWidth()-g.getFontMetrics().stringWidth(getText()))/2;
        String text;
        if (this.isToggleButton() && !this.isSelected() && unselectedText!=null)
            text = unselectedText;
        else text = getText();
        g.drawString(text, textX, textY);
    }

    @Override
    protected Dimension calculateSize() {
        String testText = (getText()==null?" button ":getText());
        if (this.getGraphics()==null || this.getGraphics().getFontMetrics()==null) return this.getSize();

        int w = this.getGraphics().getFontMetrics().stringWidth(testText) + this.getMargin().left + getMargin().right;
        int h = this.getGraphics().getFontMetrics().getHeight() + this.getMargin().top + this.getMargin().bottom;
        return new Dimension(w,h);
    }

    @Override
    public void setMargin(Insets m) {
        super.setMargin(m);
        calculateSize();
        if (this.getParent()!=null) this.getParent().validate();

    }

    @Override
    public void setFont(Font font) {
        super.setFont(font);
        if (this.getGraphics()!=null) {
            this.getGraphics().setFont(font);
            recalculateSize();
        }
    }

    @Override
    public void setText(String text) {
        super.setText(text);
        if (this.getGraphics()!=null) {
            recalculateSize();
        }
    }

    /**
     * 
     * @param text A String to display when the button is not selected. If set to null, it will remain the same as the button text.
     */
    public void setUnselectedText(String text) {
        this.unselectedText = text;
    } public String getUnselectedText() { return unselectedText; }

    public void recalculateSize() {
        Dimension size = calculateSize();
        this.setPreferredSize(size);
        this.setMinimumSize(size);
        this.setMaximumSize(size);
        this.setSize(size);
        this.getParent().validate();
    }
}
